<?php

namespace App\Models;

class Notice extends BaseModel
{
    protected $table = 'notices';
    
    // 通知阅读状态常量
    const STATUS_UNREAD = 0;  // 未读
    const STATUS_READ = 1;    // 已读

    /**
     * 获取表字段
     * @return array
     */
    protected function columns(): array
    {
        return [
            'id',
            'title',
            'content',
            'url',
            'admin_id',
            'status',
            'created_at',
            'updated_at',
            'deleted_at'
        ];
    }
    
    /**
     * 重写查询构造方法
     * @param array $params 查询参数
     * @return \Illuminate\Database\Eloquent\Builder
     */
    protected function buildQuery(array $params = []): \Illuminate\Database\Eloquent\Builder
    {
        $query = parent::buildQuery($params);
        
        // 如果指定了admin_id，则查询该管理员的消息或全局消息
        if (!empty($params['current_admin_id'])) {
            $query->where(function ($q) use ($params) {
                $q->where('admin_id', $params['current_admin_id'])
                  ->orWhere('admin_id', 0); // 全局消息 (0表示全局消息)
            });
        }
        
        // 时间范围筛选
        if (!empty($params['start_time'])) {
            $query->where('created_at', '>=', strtotime($params['start_time']));
        }
        if (!empty($params['end_time'])) {
            $endTime = strtotime($params['end_time']) + 86400 - 1;
            $query->where('created_at', '<=', $endTime);
        }
        
        return $query;
    }
}